<?php
namespace Store\Controller;
use Think\Controller;
class OrderController extends CommonController {
  function order_list(){
  	 	$storeArray  =   $this->storeArray;
  	 	$uid=$storeArray['id'];
  		$order=M('order');
		$order_desc='order_id desc';
  		if($_GET['status']!=null){
  			$status=I('get.status');
  			
  			if($status=='10'){$sx_where=" and order_state='10' and refund_state=0";$where['order_state']='10';$where['refund_state']='0';}
  			if($status=='20'){$sx_where=" and order_state='20' and refund_state=0";$where['order_state']='20';$where['refund_state']='0';}
  			if($status=='30'){$sx_where=" and order_state='30' and refund_state=0";$where['order_state']='30';$where['refund_state']='0';}
  			if($status=='40'){$sx_where=" and order_state='40' and refund_state=0";$where['order_state']='40';$where['refund_state']='0';}
  			if($status=='0'){$sx_where=" and order_state='0'";$where['order_state']='0';}
  		}
  		
  		if($_GET['refund_state']!=null){
  			$sx_where=" and refund_state>0 and refund_state<10";
  			$where['refund_state']=array('between','1,10');
			$order_desc='refund_state asc ,order_id desc';
  		}
  		
  		if($_GET['startDate']&&$_GET['endDate']){
  			$startDate=strtotime($_GET['startDate']);
  			$endDate=strtotime($_GET['endDate']);
  			
  			$sx_where.=" and add_time>='".$startDate."' and add_time<='".$endDate."'";
  			$where['add_time']=array('between',array($startDate,$endDate));
  		}
  		if($_GET['startDate']&&$_GET['endDate']==null){
  			  $startDate=strtotime($_GET['startDate']);
  				$sx_where.=" and add_time>='".$startDate."'";
  				$where['add_time']=array('EGT',$startDate);
  		}
  		if($_GET['startDate']==null&&$_GET['endDate']){
  			  $endDate=strtotime($_GET['endDate']);
  				$sx_where.=" and add_time>='".$endDate."'";
  				$where['add_time']=array('ELT',$endDate);
  		}
  		if($_GET['userName']){
  				$sx_where.=" and buyer_name='".$_GET['userName']."'";
  				$where['buyer_name']=$_GET['userName'];
  		}
  		if($_GET['orderNo']){
  				$sx_where.=" and order_sn='".$_GET['orderNo']."'";
  				$where['order_sn']=$_GET['orderNo'];
  		}
  		if($_GET['receiverMobile']){
  				$sx_where.=" and buyer_mobile='".$_GET['receiverMobile']."'";
  				$where['buyer_mobile']=$_GET['receiverMobile'];
  		}
  		if($_GET['payment_type']){
  				$sx_where.=" and payment_code='".$_GET['payment_type']."'";
  				$where['payment_code']=$_GET['payment_type'];
  		}
  		
  		
    	//全部
    	$count=$order->where('store_id='.$uid.$sx_where)->count();
    	$this->assign('counts',$count);
    	$where['store_id']=$uid;
    	$Page = new \Think\Page ($count,4); // 实例化分页类 传入总记录数
		  $Page->setConfig('header','条记录');
	    $Page->setConfig('prev','上一页');
	    $Page->setConfig('next','下一页');
		  $show = $Page->show(); // 分页显示输出
		  $order_list=$order->where($where)->limit($Page->firstRow.','.$Page->listRows)->order($order_desc)->select();
    	$this->assign('order_list',$order_list);
    	$this->assign('page',$show);
    	$this->assign('count',$count);
    	
  	  $this->display();
  }
  
  function get_delivery(){
  	if($_POST){
  		$order_id=I('post.order_id');
  		$express=M('express');
  		$express_array=$express->select();
  		$order=M('order');
  		$order_info=$order->find($order_id);
  		$ProvinceInfo=getProvinceInfo($order_info['buyer_province']);
			$CityInfo=getCityInfo($order_info['buyer_city']);
			$DistrictInfo=getDistrictInfo($order_info['buyer_district']);
			$address='<br/>姓名：'.$order_info['buyer_name'].'<br/>手机：'.$order_info['buyer_mobile'].'<br/>地址：'.$ProvinceInfo['province_name'].$CityInfo['city_name'].$DistrictInfo['district_name'].$order_info['buyer_address'];
			$data['express_company_list']=$express_array;
			$data['order_info']['address']=$address;
			$this->ajaxReturn($data);
			
  	}
  }
  function put_delivery(){
  	if($_POST){
  		$storeArray  =   $this->storeArray;
  		$uid=$storeArray['id'];
  		$order_id=I('post.order_id');
			$data['shipping_name']=I('post.express_name');
			$data['shipping_company_id']=I('post.express_company_id');
			$data['shipping_code']=I('post.express_no');
			$data['order_state']='30';
			$data['shipping_time']=time();
			$order=M('order');
		
			$rs=$order->where("store_id=$uid and order_id=$order_id")->save($data);
			
			if($rs){
				$order_info=$order->find($order_id);
				$user_info=M('users')->find($order_info['buyer_id']);
				
				if($user_info['wx_h5openid']){
					
					$openid=$user_info['wx_h5openid'];
					$url='http://m.58daiyan.com/user/order_info/id/'.$order_id;
					$keyword1=$order_info['order_sn'];
					$keyword2=get_wuliu_info($order_info['shipping_company_id']);
					$keyword3=$order_info['shipping_code'];
					send_wuliu($openid,$url,$keyword1,$keyword2,$keyword3,$keyword4);
				}
			}else{
                $this->ajax_r(0,"发货失败");
			}
            $this->ajax_r(1,"发货成功");
  	}
  }
  function order_info(){
  		$storeArray  =   $this->storeArray;
  		$uid=$storeArray['id'];
    	$order_id=I('get.id');
    	$order=M('order');
    	$order_goods=M('order_goods');
    	$order_info=$order->where('store_id='.$uid.' and order_id='.$order_id)->find();
    	$order_goods_info=$order_goods->where('order_id='.$order_id)->select();
    	$order_price_action=M('order_price_action')->where('order_id='.$order_id)->order('action_id desc')->find();
    	$this->assign('order_price_action',$order_price_action);
    	$this->assign('order_goods_info',$order_goods_info);
    	$this->assign('order_info',$order_info);
    	
    	$this->display();
  }
  
  function get_orderlogistics(){
  	if($_POST){
  		$order_id=I('post.order_id');
  		$order=M('order');
  		$order_info=$order->find($order_id);
  		$result=getOrderTracesByJson($order_info['shipping_company_id'],$order_info['shipping_code'],'1304146','f0e280a8-d7b0-470a-b1bd-8d4a9584a78e','http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx');
  		$this->ajaxReturn($result);
  	}
  }
  
  function order_return(){
  	$order=M('order');
  	$users=M('users');
  	$order_id=I('get.order_id');
  	$order_info=$order->find($order_id);
  	$status=I('get.status');
  	$data['order_id']=$order_id;
  	$data['refund_agreen_time']=time();
  	$storeArray  =   $this->storeArray;
  	$store_id=$storeArray['id'];
  	if($status==3){
  		$data['refund_state']=3;
  	}
  	if($status==2){
  		if($order_info['refund_type']==1){
  			$data['refund_state']=4;
  		}else{
  			$data['refund_state']=2;
  		}
  	}
  	if($status==4){
  		  $data['refund_state']=4;
  	}
  	$order->save($data);
  	$this->redirect('order/order_info',array('id'=>$order_id));
  }
  function back_retun(){
  	$order=M('order');
  	$data['order_id']=I('get.order_id');
  	$data['refund_state']=I('get.type');
  	$data['refund_agreen_time']=time();
  	if($data['refund_state']==3||$data['refund_state']==2||$data['refund_state']==4){
  		$order_info=$order->find($data['order_id']);
  		if($data['refund_state']==2&&$order_info['refund_type']==1){
  			$data['refund_state']=4;
  		}
  		$rs=$order->save($data);
  	}
  	if($rs){
        $this->ajax_r(1,"操作成功");
  	}else{
        $this->ajax_r(0,"操作失败");
  	}
  	
  }
  function edit_price(){
  	$storeArray  =   $this->storeArray;
  	$uid=$storeArray['id'];
  	$price=I('post.price');
  	$order_id=I('post.order_id');
  	$order_info=M('order')->find($order_id);
  	
  	print_r($_POST);
  	$log['order_id']=$order_id;
  	$log['action']='修改订单价格：原价-￥'.$order_info['order_amount'].',修改后￥'.$price;
  	$log['uid']=$uid;
  	$log['user_name']='商家';
  	$log['action_time']=time();
  	$log['yprice']=$order_info['order_amount'];
  	$log['xprice']=$price;
  	
  	$data['order_id']=$order_id;
  	$data['order_amount']=$price;
  	$rs=M('order')->save($data);
  	//print_r(M('order')->getLastSql());
  	if($rs){
  		$log_info=M('order_price_action')->add($log);
  		if($log_info){
            $this->ajax_r(1,"修改成功");
  		}else{
            $this->ajax_r(0,"修改失败");
  		}
  	}else{
        $this->ajax_r(0,"修改失败");
  	}
  	
  }
}
